package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.*;
import com.kindergarten.service.IChildrenService;
import com.kindergarten.service.IClassesService;
import com.kindergarten.service.IUserChildrenService;
import com.kindergarten.service.IUserService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-27
 */
@Controller
@RequestMapping("/children-api")
public class ChildrenController {

    @Autowired
    IChildrenService childrenService;

    @Autowired
    IClassesService classesService;

    @Autowired
    IUserService iUserService;

    @Autowired
    IUserChildrenService iUserChildrenService;

    /**
     * 01-跳转到模板引擎页面
     * @return
     */
    @GetMapping("topage")
    @Secured(value = {"ROLE_超级管理员","ROLE_教师"})
    public String topage(Model model) {
        List<Classes> classesList = classesService.list(new QueryWrapper<Classes>().eq("del", 0));
        model.addAttribute("claObjects",classesList);
        return "stuPage";
    }

    /**
     * 02-数据加载分页
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    @Secured(value = {"ROLE_超级管理员","ROLE_教师"})
    public MMGridPageVoBean page(Children children) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();

        QueryWrapper<Children> queryWrapper = new QueryWrapper<Children>();
        queryWrapper.orderByDesc("entrance");
        queryWrapper.eq("del", 0); //删除不查询

        queryWrapper.like(children.getName() != null && !"".equals(children.getName()), "name", children.getName());
        queryWrapper.eq(children.getClassName() != null && !"-1".equals(children.getClassName()), "class_id", children.getClassName());

        IPage<Children> page = childrenService.page(new Page<Children>(children.getPage(), children.getLimit()), queryWrapper);

        List<Children> records = page.getRecords();
        for (Children record : records) {
            Classes classes = classesService.getById(record.getClassId());
            record.setClassName(classes.getName());
        }
        pageVoBean.setRows(records); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }

    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toaddpage")
    public String toaddpage(Model model) {
        //班级
        List<Classes> classesList = classesService.list(new QueryWrapper<Classes>().eq("del", 0));
        //家长集合
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "3"));
        model.addAttribute("claObjects",classesList);
        model.addAttribute("userObjects",userList);
        return "stuPageAdd";
    }

    /**
     * 新增学生
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public DataResults add(Children children, UserChildren userChildren){
        childrenService.add(children,userChildren);
        return DataResults.success(ResultCode.SUCCESS);
    }

    /**
     * 02-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("toupdatepage")
    public String toupdatepage(Integer id, Model model) {
        //数据回显用的
        Children children = childrenService.getById(id);
        //班级
        List<Classes> classesList = classesService.list(new QueryWrapper<Classes>().eq("del", 0));
        //家长集合
        List<User> userList = iUserService.list(new QueryWrapper<User>().eq("del", 0).eq("user_state", "3"));
        model.addAttribute("claObjects",classesList);
        model.addAttribute("userObjects",userList);
        model.addAttribute("manageStu", children);

        UserChildren userChildren = iUserChildrenService.getOne(new QueryWrapper<UserChildren>().eq("children_id", id));
        model.addAttribute("uc", userChildren);
        return "stuPageUpdate";  //模板引擎页面
    }

    /**
     * 更新学生
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public DataResults update(Children children, UserChildren userChildren){
        childrenService.update(children,userChildren);
        return DataResults.success(ResultCode.SUCCESS);
    }

    /**
     * 删除
     *
     * @return
     */
    @ResponseBody
    @DeleteMapping("delete/{id}")
    public DataResults delete(@PathVariable("id") Integer id) {
        childrenService.delete(id);
        return DataResults.success(ResultCode.SUCCESS);
    }

}
